package com.yfbao.horizon.schedule.tk.utils;

import cn.hutool.core.io.FileUtil;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.yfbao.horizon.schedule.tk.handler.DefaultWordAllow;
import com.yfbao.horizon.schedule.tk.handler.DefaultWordDeny;
import com.yfbao.horizon.schedule.tk.handler.DefaultWordDenyTest;
import com.yfbao.horizon.schedule.tk.handler.DefaultWordReplace;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class CheckUtil {

    private static SensitiveWordBs wordBs = SensitiveWordBs.newInstance()
            .ignoreCase(true)
            .ignoreWidth(true)
            .ignoreNumStyle(true)
            .ignoreChineseStyle(true)
            .ignoreEnglishStyle(true)
            .ignoreRepeat(true)
            .enableNumCheck(false)
            .enableEmailCheck(false)
            .enableUrlCheck(false)
            .enableWordCheck(true)
            .init();
    private static List<String> textList = new ArrayList<>();

    public static void main(String[] args) {
//        initTextList();
//        initTextFile();
//        chendDef2();
//        chendDef();
//        chendDef3();
//        chendDef4();
//        checkChart();
        checkChartBigFile();

//        htmlparse();

//        testMemory();

//        checkAndReplace(textList);
    }

    private static void htmlparse(){

        int a=100;
        if(a>50){
            System.out.println("1");
        }else if(a>10){
            System.out.println("2");
        }else if(a>0){
            System.out.println(3);
        }

        String htmlContent = "<h3 style=\"margin-left:0px;\">定义</h3><p style=\"margin-left:0px;\">文字是人类用表义符号记录表达信息以传之久远的方式和工具。现代文字多是记录语言的工具，人类往往先有口头的语言后产生书面的文字，很多<a href=\"https://baike.baidu.com/item/%E5%B0%8F%E8%AF%AD%E7%A7%8D/1618699?fromModule=lemma_inlink\">小语种</a>，有语言但没有文字。</p><p style=\"margin-left:0px;\"><strong>文与字</strong></p><p style=\"margin-left:0px;\">东汉<a href=\"https://baike.baidu.com/item/%E8%AE%B8%E6%85%8E/577196?fromModule=lemma_inlink\">许慎</a>《〈<a href=\"https://baike.baidu.com/item/%E8%AF%B4%E6%96%87%E8%A7%A3%E5%AD%97/6180?fromModule=lemma_inlink\">说文解字</a>〉叙》：“盖依类象形，故谓之文；其后形声相益，即谓之字，”也就是说，“文”是独体字（包含<a href=\"https://baike.baidu.com/item/%E8%B1%A1%E5%BD%A2%E5%AD%97/257491?fromModule=lemma_inlink\">象形字</a>和<a href=\"https://baike.baidu.com/item/%E6%8C%87%E4%BA%8B%E5%AD%97/7659536?fromModule=lemma_inlink\">指事字</a>），而“字”是由<a href=\"https://baike.baidu.com/item/%E7%8B%AC%E4%BD%93%E5%AD%97/6060865?fromModule=lemma_inlink\">独体字</a>组合的<a href=\"https://baike.baidu.com/item/%E5%90%88%E4%BD%93%E5%AD%97/2459569?fromModule=lemma_inlink\">合体字</a>（包含<a href=\"https://baike.baidu.com/item/%E4%BC%9A%E6%84%8F%E5%AD%97/7146699?fromModule=lemma_inlink\">会意字</a>、<a href=\"https://baike.baidu.com/item/%E5%BD%A2%E5%A3%B0%E5%AD%97/6106941?fromModule=lemma_inlink\">形声字</a>、<a href=\"https://baike.baidu.com/item/%E5%81%87%E5%80%9F%E5%AD%97/5576944?fromModule=lemma_inlink\">假借字</a>）。</p><p style=\"margin-left:0px;\">&nbsp;</p><h3 style=\"margin-left:0px;\">分类</h3><p style=\"margin-left:0px;\">文字按字音和字形，可分为<a href=\"https://baike.baidu.com/item/%E8%A1%A8%E5%BD%A2%E6%96%87%E5%AD%97/8921177?fromModule=lemma_inlink\">表形文字</a>";
        String charToFind="</";
        int index = htmlContent.indexOf(charToFind);
        int lastCheck =0 ;
        int length = htmlContent.length();
        while (index >= 0) {
            if(index-lastCheck>512){
                String substring = htmlContent.substring(lastCheck, index+1);
                System.out.println(substring);

                lastCheck=index+1;
            }
            System.out.println("Index of '" + charToFind + "' at: " + index);
            index = htmlContent.indexOf(charToFind, index + 1);
        }
        if(length-lastCheck>0){
            String substring = htmlContent.substring(lastCheck, length);
            System.out.println(substring);
        }

        // 解析HTML内容
//        Document doc = Jsoup.parse(htmlContent);
//        String textContent = doc.text();
//        System.out.println(textContent);
//        // 获取所有文本内容
//        List<Node> nodes = doc.childNodes();
//        for (Node node : nodes) {
//            System.out.println(node.outerHtml());
//        }

    }

    private static void checkChartBigFile(){
        String text= "五星红旗迎风飘扬，毛主席的new3画像newNumber屹立在天安门前飞翔各New,FirearmS，哈哈哈哈" +
                ",abc";
        SensitiveWordBs sensitiveWordBs = wordBs.wordDeny(new DefaultWordDenyTest())
                .wordReplace(new DefaultWordReplace())
                .wordAllow(new DefaultWordAllow())
                .init();
        String file ="C:\\Users\\byf78\\Desktop\\日志分析\\sent-da.txt";
        String outfile ="C:\\Users\\byf78\\Desktop\\日志分析\\out-sent-da.txt";
        BufferedWriter writer = null;
        InputStream tempFileInputStream = null;
        BufferedReader reader= null;
        long start = System.currentTimeMillis();
        try {
            reader = new BufferedReader(new FileReader(file));
             writer = new BufferedWriter(new FileWriter(outfile));

            Map<String, List<String>> resMap =new HashMap<>();
            StringBuilder builder =new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
//                sensitiveWordBs.findAll(line);
                builder.append(line).append("\r\n");

                if (builder.length() > 1024) {
                    //防止字符串过长，切分进行检查
                    String htmlContent = builder.toString();
                    String charToFind = "</";
                    int index = htmlContent.indexOf(charToFind);
                    int lastCheck = 0;
                    int length = htmlContent.length();
                    while (index >= 0) {
                        if (index - lastCheck > 512) {
                            String substring = htmlContent.substring(lastCheck, index + 1);
                            List<String> all = sensitiveWordBs.findAll(substring);
                            System.out.println(substring);

                            lastCheck = index + 1;
                        }
                        index = htmlContent.indexOf(charToFind, index + 1);
                    }
                    if (length - lastCheck > 0) {
                        String substring = htmlContent.substring(lastCheck, length);
                        List<String> all = sensitiveWordBs.findAll(substring);
                        System.out.println(substring);
                    }
                    builder = new StringBuilder();
                    continue;
                } else if (builder.length() > 128) {
                    List<String> all = sensitiveWordBs.findAll(builder.toString());
                    builder = new StringBuilder();
                }
            }
            if(builder.length()>0){
                List<String> all = sensitiveWordBs.findAll(builder.toString());
                builder =new StringBuilder();
            }
        }catch (Exception e){

        }finally {
            try {
                reader.close();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("cost:"+(System.currentTimeMillis()-start));

    }

    private static void checkChart(){
        String text= "五星红旗迎风飘扬，毛主席的new3画像newNumber屹立在天安门前飞翔各New,FirearmS，哈哈哈哈" +
                ",abc";
        SensitiveWordBs sensitiveWordBs = wordBs.wordDeny(new DefaultWordDenyTest())
                .wordReplace(new DefaultWordReplace())
                .wordAllow(new DefaultWordAllow())
                .init();
        List<String> all = sensitiveWordBs.findAll(text);
            log.info("检索的敏感词："+all);
    }

    private static void initTextList(){
        List<String> stringList = FileUtil.readLines("测试文本.txt", "utf-8");
        int countNum = 1;
        for(int i=0;i<countNum;i++){
            textList.addAll(stringList);
        }
        log.info("total text:"+textList.size());
    }

    private static void initTextFile(){
        List<String> stringList = FileUtil.readLines("测试文本2.txt", "utf-8");
        int countNum = 1;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < stringList.size(); i++) {
            builder.append(stringList.get(i));
            if(i<(stringList.size()-1)){
                builder.append("\r\n");
            }
        }
        textList.add(builder.toString());
        log.info("total text:"+textList.size());
    }

    private static void checkAndReplace(List<String> textList){
        SensitiveWordBs sensitiveWordBs = wordBs.wordDeny(new DefaultWordDeny()).init();

        long start = System.currentTimeMillis();
        for (String s : textList) {
            List<String> all = sensitiveWordBs.findAll(s);
//            log.info("检索的敏感词："+all);

            String replace = sensitiveWordBs.replace(s);
            log.info("替换敏感词后的结果："+replace);
        }
        log.info("total cost:"+(System.currentTimeMillis()-start)+"ms");

    }

    private static void chendDef(){
        String text= "五星红旗迎风飘扬，毛主席的画像屹立在天安门前飞翔各";
        SensitiveWordBs sensitiveWordBs = wordBs.wordDeny(new DefaultWordDeny())
                .wordReplace(new DefaultWordReplace())
                .wordAllow(new DefaultWordAllow())
                .init();
        List<String> stringList = sensitiveWordBs.findAll(text);
        log.info(stringList.toString());
        String replace = sensitiveWordBs.replace(text);
        log.info("repalce res:"+replace);
    }

    private static void chendDef4(){
        String text= "五星红旗迎风飘扬，~`!@#$%^&*()_+-=[]{}\\|:;'\",飘扬.<>?/毛主席的画像屹立在天安门前飞翔各";
        SensitiveWordBs sensitiveWordBs = wordBs.wordDeny(new DefaultWordDeny())
                .wordReplace(new DefaultWordReplace())
                .wordAllow(new DefaultWordAllow())
                .init();
        List<String> stringList = sensitiveWordBs.findAll(text);
        log.info(stringList.toString());
        String replace = sensitiveWordBs.replace(text);
        log.info("repalce res:"+replace);
    }

    private static void chendDef2(){
        String text= "五星红旗迎风飘扬，毛主席的画像屹立在天安门前飞翔";
        List<String> stringList = wordBs.findAll(text);
        log.info(stringList.toString());
    }

    private static void chendDef3(){
        String text= "五星红旗迎风飘扬，毛主席的画像屹立在天安门前";
        wordBs.wordReplace(new DefaultWordReplace()).init();
        String replace = wordBs.replace(text);
        log.info(replace);
//        wordBs.findAll()
    }

    private static void testMemory(){
        log.info("begin test memory");
        long totalMemory = Runtime.getRuntime().totalMemory();
        log.info("Total Memory: " + totalMemory / (1024 * 1024) + " MB");
        Map<String,SensitiveWordBs> map = new HashMap<>();
        long start = System.currentTimeMillis();
        int total=100;
        for(int i=0;i<100;i++){
//            SensitiveWordBs init = SensitiveWordBs.newInstance().init();
            SensitiveWordBs init = SensitiveWordBs.newInstance().wordDeny(new DefaultWordDeny()).wordReplace(new DefaultWordReplace()).init();
            map.put(String.valueOf(i),init);
            if(i==(total/2)){
                try {
                    long totalMemory1 = Runtime.getRuntime().totalMemory();
                    log.info((int)(total/2)+", Total Memory: " + totalMemory1 / (1024 * 1024) + " MB");
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        long totalMemory2 = Runtime.getRuntime().totalMemory();
        log.info(total+",, Total Memory: " + totalMemory2 / (1024 * 1024) + " MB");
        log.info(total+", Total cost: " + (System.currentTimeMillis()-start)+"ms");
        log.info("finish all memory");
    }


}
